package cn.czh.controller;

import cn.czh.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
// 当前类统一托底方法
@DefaultProperties(defaultFallback = "fallbackMethod")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/getById/{id}")
    /*@HystrixCommand(fallbackMethod = "fallbackgetById")*/
    @HystrixCommand //// 标记方法熔断
    public User getById(@PathVariable("id") Long id){
        // 使用RestTemplate调用，直接调用
        // String url = "http://localhost:1020/getById/" + id;
        // 负载均衡调用user-server服务的集群服务
        String url = "http://user-server/getById/" + id;
        return restTemplate.getForObject(url,User.class);
    }

   /* public User fallbackgetById(@PathVariable("id") Long id){
        return new User(-1L,"服务熔断开启","该服务不可使用");
    }*/


    // 统一降级方法
    public User fallbackMethod(){
        return new User(-1L,"服务熔断开启","该服务不可使用!");
    }
}
